#include"message.h"

#include<sstream>
 std::string  Message::formatFeildWithColor(COLOR_NUMBER colerNumber,bool dark,const std::string delimiter,const std::string& fileName,bool colored )
 {
   std::stringstream ss;
     if(colored)
     {
      ss<<"\033[" << dark<<";"<<colerNumber<<"m";
     }
     ss<<fileName;
     if(colored)
     {
      ss<<"\033[0m";//重置属性
     }
     ss<<delimiter;
     return ss.str();
 }
   
 Message::Message(LOG_LEVEL level,const std::string& fileName,const std::string& functionName ,int lineNumber, bool colored )
{
    coloredHeader_ = FormatHeader(level,fileName,functionName,lineNumber,true);
    commonHeader_ = FormatHeader(level,fileName,functionName,lineNumber,false);
 }
 //[时间戳][日志级别][文件名称][函数名称][行号]
std::string  Message::FormatHeader(LOG_LEVEL level, std::string fileName,const std::string& functionName ,int lineNumber, bool colored )
{
   //时间戳
   std::stringstream ss;
   ss<<formatFeildWithColor(DEFAULT_COLOR,0," ",TimeStamp().Now().ToString(),colored);
  
   //日志级别  
   COLOR_NUMBER logColorNumber =LogLevelColor[level];
   std::string logLevelString = LogLevelString[level];
   ss << formatFeildWithColor(logColorNumber,1," ",logLevelString,colored);

   //文件名称
   int pos =0;//文件名前面的第一个'/'
   for(int i= fileName.size()-1;i>=0;--i)
   {
      if('/' == fileName[i])
      {
        pos =i;
        break;
      }
   }
   fileName = fileName.substr(pos+1,fileName.size()-1);
   COLOR_NUMBER fileColor = LogFiledColor[FILE_NAME];
   ss<< formatFeildWithColor(fileColor,1," ","["+fileName+"]",colored);

   //函数名称
   COLOR_NUMBER functionColor = LogFiledColor[FUNCTION_NAME];
    ss<< formatFeildWithColor(functionColor,1," ","["+functionName+"]",colored);

   //行号
   COLOR_NUMBER linecolor  = LogFiledColor[LINE_NUMBER];
    ss<< formatFeildWithColor(linecolor,1," : ",std::to_string (lineNumber),colored);

    return ss.str();
}
 std::string Message::ToString(bool colored) const
 {
    return colored? coloredHeader_ + msg_ :commonHeader_ + msg_;
 }



